import { createRouter,createWebHistory } from "vue-router";
import { getCookies } from '@/utils/cookie';

import LayoutMain from '@/layout/index.vue';

const routes = [
    {
        path:'/',
        meta: {
            title: 'XY',
        },
        component: LayoutMain,
    },
    {
        path:'/login',
        name:'login',
        meta: {
            title: '登录',
            hideInMenu: true,
        },
        component:()=>import('@/pages/login/index.vue'),
    }
];

const router = createRouter({
    history:createWebHistory(import.meta.env.BASE_URL),
    routes
});

/**
 * 路由拦截
 * 权限验证
 */
router.beforeEach(async (to, from, next)=>{
    // 判断是否需要登录才可以进入
    if(to.matched.some((elem) => elem.meta.auth)){
        const token = getCookies('token');
        if(token && token !== 'undefined'){
            //  判断是否有权限 前端权限和后端权限   暂时预留
            next();
        }
        else
        {
            // 没有登录的时候跳转到登录界面
            // 携带上登陆成功之后需要跳转的页面完整路径
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath,
                },
            });
        }
    }
    else
    {
        // 不需要身份校验 直接通过
        next();
    }
});
export default router